<template>
  <div id="userBirday-container">
    <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate" @cancel="$emit('close')" @confirm="onConfirm" />
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { updateUserProfile } from '@/api/user.js'
import { Toast } from 'vant'

export default {
  name: 'userBirday',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      currentDate: new Date(this.value),
      minDate: new Date('1900-01-01'),
      maxDate: new Date()
    }
  },
  created () {},
  methods: {
    async onConfirm (val) {
      const formatTime = dayjs(val).format('YYYY-MM-DD')
      // console.log(formatTime)
      Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '更新中....'
      })
      try {
        await updateUserProfile({
          birthday: formatTime
        })
        Toast.success('更新成功')
        this.$emit('input', formatTime)
        this.$emit('close')
      } catch (err) {
        Toast.fail('更新失败')
      }
    }
  }
}
</script>

<style lang='less' scoped>
</style>
